package com.yx.learn.service.impl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yx.learn.dao.AuthorityDao;
import com.yx.learn.domain.Authority;
import com.yx.learn.service.AuthorityService;
import com.yx.learn.util.TreeAndToJsonUtil;


@Service("authorityService")
@Transactional
public class AuthorityServiceImpl implements AuthorityService {
	
	@Autowired
	@Qualifier("authorityDao")    
	private AuthorityDao authorityDao;

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public JSONArray getAuthoritiesByIdJson(int id) throws Exception {
		// TODO Auto-generated method stub
		TreeAndToJsonUtil js=new TreeAndToJsonUtil();
		JSONArray ja = new JSONArray();
		List<Authority> list = authorityDao.getAuthoritiesByPid(id);
		for(int i=0;i<list.size();i++){
			Boolean hasChildren = false;
			if(authorityDao.getAuthoritiesByPid(list.get(i).getId()).size()>0){
				hasChildren = true;
			}
			JSONObject jsonObject=js.authorityToJsonObject(list.get(i), hasChildren);
			ja.add(jsonObject);
		}
		return ja;

	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public JSONArray getAuthoritiesByConditionJson(String authorityName, String flag)
			throws Exception {
		// TODO Auto-generated method stub
		StringBuffer hql=new StringBuffer();
		hql.append("from Authority a where 1=1 ");
		if(authorityName!=null&&authorityName.length()>0){
			hql.append("and a.authorityName='").append(authorityName).append("'");
			if(flag!=null&&flag.length()>0){
				hql.append("and a.flag='").append(flag).append("'");
			}
		}else{
			if(flag!=null&&flag.length()>0){
				hql.append("and a.flag='").append(flag).append("'");
				hql.append("and a.paid='").append((-1)).append("'");
			}
		}
		String s = hql.toString();
		TreeAndToJsonUtil js=new TreeAndToJsonUtil();
		JSONArray ja = new JSONArray();
		List<Authority> list = authorityDao.getAuthoritiesByCondition(s);
		for(int i=0;i<list.size();i++){
			Boolean hasChildren = false;
			if(authorityDao.getAuthoritiesByPid(list.get(i).getId()).size()>0){
				hasChildren = true;
			}
			JSONObject jsonObject=js.authorityToJsonObject(list.get(i), hasChildren);
			ja.add(jsonObject);
		}
		return ja;
	}

	public Authority findById(int id) throws Exception {
		// TODO Auto-generated method stub
		return this.authorityDao.findById(id);
	}

	public void saveAuthority(Authority authority) throws Exception {
		// TODO Auto-generated method stub
		this.authorityDao.save(authority);
	}

	public void updateAuthority(Authority authority) throws Exception {
		// TODO Auto-generated method stub
		this.authorityDao.update(authority);
	}

	public void deleteAuthority(Authority authority) throws Exception {
		// TODO Auto-generated method stub
		this.authorityDao.delete(authority);
	}
	
	public String getAllAuthoritiesJson(int id) throws Exception {
		// TODO Auto-generated method stub
		TreeAndToJsonUtil js=new TreeAndToJsonUtil();
		JSONArray ja = new JSONArray();
		List<Authority> list = authorityDao.getAuthoritiesByPid(id);
		for(int i=0;i<list.size();i++){
			Boolean hasChildren = false;
			if(authorityDao.getAuthoritiesByPid(list.get(i).getId()).size()>0){
				hasChildren = true;
			}
			JSONObject jsonObject=js.jsonObjectForAuthorityTree(list.get(i), hasChildren);
			ja.add(jsonObject);
		}
		return ja.toString();
	}

	

	

}
